<#
Git Hooks 安装（原生 PowerShell）
用途：安装 pre-commit 与 commit-msg 钩子，支持 PowerShell 与 Bash 双栈。
用法：
  pwsh -File scripts/windows/install-git-hooks.ps1
#>
param()

$WindowsDir = Split-Path -Parent $PSCommandPath
$ScriptsDir = Split-Path -Parent $WindowsDir
$RepoDir = Split-Path -Parent $ScriptsDir

$hooks = Join-Path $RepoDir '.git/hooks'
New-Item -ItemType Directory -Force -Path $hooks | Out-Null

function Get-HookTemplateContent([string]$Name) {
  $p = (Join-Path $RepoDir "scripts/hooks/$Name.hook.sh")
  if (Test-Path $p) { return Get-Content -Raw -Encoding UTF8 $p }
  return $null
}

$preCommitSh = Get-HookTemplateContent 'pre-commit'
if (-not $preCommitSh) {
  $preCommitSh = @(
    "#!/usr/bin/env bash",
    "set -e",
    "ROOT=\"$(git rev-parse --show-toplevel 2>/dev/null || pwd)\"",
    "SCRIPT=\"\";",
    "candidate=\"$ROOT/scripts/windows/pre-commit-check.ps1\"",
    "if [ -f \"$candidate\" ]; then SCRIPT=\"$candidate\"; fi",
    "if [ -z \"$SCRIPT\" ]; then",
    "  for d in \"$ROOT\"/*; do",
    "    if [ -f \"$d/scripts/windows/pre-commit-check.ps1\" ]; then SCRIPT=\"$d/scripts/windows/pre-commit-check.ps1\"; break; fi",
    "  done",
    "fi",
    "if [ -z \"$SCRIPT\" ] && [ -f \"$ROOT/config/rules.env\" ]; then",
    "  MIRROR_DIR_NAME=\"$(grep -E '^MIRROR_DIR_NAME=' \"$ROOT/config/rules.env\" | sed 's/^MIRROR_DIR_NAME=//')\"",
    "  if [ -n \"$MIRROR_DIR_NAME\" ] && [ -f \"$ROOT/$MIRROR_DIR_NAME/scripts/windows/pre-commit-check.ps1\" ]; then",
    "    SCRIPT=\"$ROOT/$MIRROR_DIR_NAME/scripts/windows/pre-commit-check.ps1\";",
    "  fi",
    "fi",
    "if [ -z \"$SCRIPT\" ]; then echo \"[hook] pre-commit-check.ps1 not found\" >&2; exit 1; fi",
    "pwsh -NoProfile -File \"$SCRIPT\""
  ) -join "`n"
}

$preCommitPs = @(
  "# PowerShell pre-commit (dynamic discovery)",
  "$Root = (& git rev-parse --show-toplevel 2>`$null); if (-not $Root) { $Root = (Get-Location).Path }",
  "$Script = $null",
  "$Candidate = Join-Path $Root 'scripts/windows/pre-commit-check.ps1'",
  "if (Test-Path $Candidate) { $Script = $Candidate }",
  "if (-not $Script) { foreach ($d in Get-ChildItem -Directory $Root) { $p = Join-Path $d.FullName 'scripts/windows/pre-commit-check.ps1'; if (Test-Path $p) { $Script = $p; break } } }",
  "$envFile = Join-Path $Root 'config/rules.env'; if (-not $Script -and (Test-Path $envFile)) { $mirror = ((Get-Content -Raw -Encoding UTF8 $envFile) -split "`n") | Where-Object { $_ -match '^MIRROR_DIR_NAME=' } | ForEach-Object { $_ -replace '^MIRROR_DIR_NAME=', '' }; if ($mirror) { $m = Join-Path $Root $mirror; $m = Join-Path $m 'scripts/windows/pre-commit-check.ps1'; if (Test-Path $m) { $Script = $m } } }",
  "if (-not $Script) { Write-Error '[hook] pre-commit-check.ps1 not found'; exit 1 }",
  "& pwsh -NoProfile -File $Script"
) -join "`n"

$commitMsgSh = Get-HookTemplateContent 'commit-msg'
if (-not $commitMsgSh) {
  $commitMsgSh = @(
    "#!/usr/bin/env bash",
    "set -e",
    "ROOT=\"$(git rev-parse --show-toplevel 2>/dev/null || pwd)\"",
    "SCRIPT=\"\";",
    "candidate=\"$ROOT/scripts/windows/validate-commit-msg.ps1\"",
    "if [ -f \"$candidate\" ]; then SCRIPT=\"$candidate\"; fi",
    "if [ -z \"$SCRIPT\" ]; then",
    "  for d in \"$ROOT\"/*; do",
    "    if [ -f \"$d/scripts/windows/validate-commit-msg.ps1\" ]; then SCRIPT=\"$d/scripts/windows/validate-commit-msg.ps1\"; break; fi",
    "  done",
    "fi",
    "if [ -z \"$SCRIPT\" ] && [ -f \"$ROOT/config/rules.env\" ]; then",
    "  MIRROR_DIR_NAME=\"$(grep -E '^MIRROR_DIR_NAME=' \"$ROOT/config/rules.env\" | sed 's/^MIRROR_DIR_NAME=//')\"",
    "  if [ -n \"$MIRROR_DIR_NAME\" ] && [ -f \"$ROOT/$MIRROR_DIR_NAME/scripts/windows/validate-commit-msg.ps1\" ]; then",
    "    SCRIPT=\"$ROOT/$MIRROR_DIR_NAME/scripts/windows/validate-commit-msg.ps1\";",
    "  fi",
    "fi",
    "if [ -z \"$SCRIPT\" ]; then echo \"[hook] validate-commit-msg.ps1 not found\" >&2; exit 1; fi",
    "pwsh -NoProfile -File \"$SCRIPT\" -MsgFile \"$1\""
  ) -join "`n"
}

Set-Content -Path (Join-Path $hooks 'pre-commit') -Value $preCommitSh -Encoding UTF8
Set-Content -Path (Join-Path $hooks 'pre-commit.ps1') -Value $preCommitPs -Encoding UTF8
Set-Content -Path (Join-Path $hooks 'commit-msg') -Value $commitMsgSh -Encoding UTF8

try { & chmod +x (Join-Path $hooks 'pre-commit'); & chmod +x (Join-Path $hooks 'commit-msg') } catch {}
Write-Host "已安装 Git hooks 到：$hooks"
exit 0
